<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var reverseOnlyLetters = function (S) {
      let arr = S.match(/[a-zA-Z]/g);
      if (arr) {
        arr.reverse();
        for (let i = 0; i < S.length; i++) {
          if (!(/[a-zA-Z]/.test(S[i]))) {
            arr.splice(i, 0, S[i])
          }
        }
      } else {
        arr = S.split('');
      }
      return arr.join('');
    };
    console.log(reverseOnlyLetters("Test1ng-Leet=code-Q!"))
  </script>
</body>

</html>